package com.edward.volleydemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

public class StringRequestActivity extends AppCompatActivity {

    private TextView tvResult;
    private RequestQueue mRequestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request);

        tvResult = (TextView) findViewById(R.id.tv_result);

        //1.创建 RequestQueue
        mRequestQueue = Volley.newRequestQueue(this);
    }


    //GET请求
    public void loadData(View view) {
        //2.创建 StringRequest
        //参数一:请求方法  int
        //参数二:url String
        //参数三:响应监听器  Response.Listener
        //参数四:错误监听器  Response.ErrorListener
        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                "http://www.baidu.com",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        if (s == null) {
                            return;
                        }
                        tvResult.setText("加载成功\n" + s);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        tvResult.setText("加载失败\n" + volleyError.getMessage());
                    }
                }
        );

        //3.将StringRequest对象加入到RequestQueue
        mRequestQueue.add(stringRequest);
    }


    public void loadData2(View view) {
        //2.创建StringRequest对象
        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                "http://www.baidu.com",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        if (s == null) {
                            return;
                        }
                        tvResult.setText("加载成功\n" + s);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        tvResult.setText("加载失败\n" + volleyError.getMessage());
                    }
                }) {

            //重写getParams方法
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                //创建map
                Map<String, String> paramsMap = new HashMap<>();
                //添加值
                paramsMap.put("key", "value");
                //将map返回
                return paramsMap;
            }
        };

        //3.将Request对象加入RequestQueue
        mRequestQueue.add(stringRequest);
    }

}
